package com.ak.kaoyan.utils;

import com.ak.kaoyan.entity.Judge_Info;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.UUID;


@Slf4j
@Component
public class ExecuteUtils {

    @Value("${judge.sys.tests-path}")
    private String sysTestsPath;

    @Value("${judge.sys.subs-path}")
    private String sysSubsPath;


    @Value("${judge.sys.username}")
    private String systemUsername;


    @Value("${judge.sys.password}")
    private String systemPassword;


    public Judge_Info execute_java(String input, String code, byte[] classByte) {

        // 创建输入文件,创建输入文件
        String currentPath = System.getProperty("user.dir");
        String pid = UUID.randomUUID().toString();
        Path dir = Paths.get(currentPath, "test", pid);
        Path out_path = Paths.get(currentPath, "test", pid, "output.txt");
        Path input_path = null;
        // input不为空
        if (!"".equals(input) && input != null) {
            // 生成输入文件
            Paths.get(currentPath, "test", pid, "input.txt");
            FileUtil.writeInput(input, input_path.toString());
        }
        // 生成.class 文件
        String className = StringUtils.getClassName(code);
        Path class_path = Paths.get(currentPath, "test", pid, className + ".class");
        log.info(".class 文件路径" + class_path);
        FileUtil.writeByte(classByte, class_path.toString());

        // 运行的命令行
        String cmd = "java -classpath " + dir.toString() + " " + className;
        log.info("运行的命令  " + cmd);

        // 默认运行最长时间为10秒,运行内存给10Mb
        Map<String, Object> result = null;

        // 如果有输入
        if (!"".equals(input) && input != null) {
            result = runnerGetRuntimeResult(cmd,
                    systemUsername,
                    systemPassword,
                    input_path.toString(),
                    out_path.toString(),
                    10000,
                    10 * 1000);
        } else {
            result = runnerGetRuntimeResult(cmd,
                    systemUsername,
                    systemPassword,
                    null,
                    out_path.toString(),
                    10000,
                    10 * 1000);
        }
        String re = FileUtil.readFile(out_path.toString());
        log.info("结果为 " + re);
        int runtimeResultSlug = (Integer) result.get("runtimeResult");
        int usedTime = (Integer) result.get("usedTime");
        int usedMemory = (Integer) result.get("usedMemory");

        log.info(result.toString());
        return null;
    }


//    /**
//     * 根据代码类型，获取文件，比如文件路径为C:\Users\Joker\Desktop\helloword.class
//     *
//     * @param type                   代码类型
//     * @param classpath_No_Extension 文件名称但是不带扩展  C:\Users\Joker\Desktop\helloword
//     * @param file_dir               文件所在路径 C:\Users\Joker\Desktop
//     * @param class_name             类的名称
//     * @return
//     */
//    private String getRunCommandLine(String type,
//                                     String classpath_No_Extension,
//                                     String file_dir,
//                                     String class_name) {
//        String sampleCommand = "";
//        switch (type) {
//            case Consts.Language.C:
//                sampleCommand = "{filename}.exe";
//                break;
//            case Consts.Language.CPP:
//                sampleCommand = "{filename}.exe";
//                break;
//            case Consts.Language.GO:
//                sampleCommand = "go {filename}.go";
//                break;
//            case Consts.Language.JAVA:
////                sampleCommand =  "java -classpath {pathname} Main";
//                sampleCommand = "java -classpath " + file_dir + " " + class_name;
//                break;
//            case Consts.Language.PERL:
//                sampleCommand = "sorry, idk";
//                break;
//            case Consts.Language.PYTHON:
//                sampleCommand = "python {filename}.py";
//                break;
//            case Consts.Language.RUBY:
//                sampleCommand = "ruby {filename}.rb";
//                break;
//            default:
//                sampleCommand = "java -cp {filename}";
//                break;
//        }
//        log.info("编译命令 " + sampleCommand);
//        return sampleCommand.replaceAll("\\{filename\\}", classpath_No_Extension);
//    }

    /**
     * 获取程序运行结果.
     *
     * @param commandLine    - 待执行程序的命令行
     * @param systemUsername - 登录操作系统的用户名
     * @param systemPassword - 登录操作系统的密码
     * @param inputFilePath  - 输入文件路径(可为NULL)
     * @param outputFilePath - 输出文件路径(可为NULL)
     * @param timeLimit      - 时间限制(单位ms, 0表示不限制)
     * @param memoryLimit    - 内存限制(单位KB, 0表示不限制)
     * @return 一个包含程序运行结果的Map<String, Object>对象
     */
    public native Map<String, Object> runnerGetRuntimeResult(String commandLine,
                                                             String systemUsername,
                                                             String systemPassword,
                                                             String inputFilePath,
                                                             String outputFilePath,
                                                             int timeLimit,
                                                             int memoryLimit);

}
